<?php

namespace App\Services\Game\Data\Settlement\Elasticsearch;

use App\Core\Traits\Numeral;
use App\Services\Service;
use App\Services\Traits\DatabaseElasticsearch;
use App\Models\Elasticsearch\Settlement\Info as SettlementInfo;
use App\Exports\Game\Data\Settlement\Elasticsearch\InfoExport;

class InfoService extends Service
{
    use DatabaseElasticsearch, Numeral;

    /**
     * 导出数据
     *
     * @param array $params
     * @param \Illuminate\Http\Request $request
     * @param bool $export
     * @return false|void
     */
    private function export($params, $request, bool $export)
    {
        if (!$export) {
            return false;
        }
        // 前端排序
        $this->makeOrderParam($request, $params);
        // 第二排序
        $this->makeOrderParam($request, $params, 'id');

        // 导出文件
        InfoExport::es($params, SettlementInfo::class)->download();
    }

    /**
     * 获取数据列表
     *
     * @param \Illuminate\Http\Request $request
     * @param bool $export
     * @return array[]|int[]|array[]
     */
    public function getList($request, $export = false)
    {
        $params = ['body' => ['track_total_hits' => true]];

        // 筛选【平台】
        $this->makeWhereParamOrg($request, $params);
        // 筛选【平台】【排除】
        $this->makeWhereParamNot($request, 'plat_code_exclude', $params, 'plat_code');
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $params, null, 'datetime', SettlementInfo::INDEX_NAME);
        // 筛选【币种】
        $this->makeWhereParam($request, 'currency', $params);
        // 筛选【币种】【排除】
        $this->makeWhereParamNot($request, 'currency_exclude', $params, 'currency');
        // 筛选【状态】
        $this->makeWhereParam($request, 'status', $params);
        // 筛选【类型】
        $this->makeWhereParam($request, 'player_type', $params);
        // 筛选【游戏】
        $this->makeWhereParam($request, 'game_id', $params);
        // 筛选【游戏】【排除】
        $this->makeWhereParamNot($request, 'game_id_exclude', $params, 'game_id');
        // 筛选【场景】
        $this->makeWhereParam($request, 'scene_id', $params);
        // 筛选【搜索】
        $this->makeWhereParamSearch($request, $params);
        // 筛选【字段】
        $this->makeWhereFieldRange($request, $params);
        // 筛选【slo】【旋转类型】
        $this->makeWhereParam($request, 'ext.spin_type', $params);
        // 筛选【slo】【免费游戏】
        $this->makeWhereParam($request, 'ext.is_free', $params);
        // 筛选【slo】【触发免费游戏】
        $this->makeWhereParam($request, 'ext.is_free_trigger', $params);
        // 筛选【slo】【触发免费游戏来源】
        $this->makeWhereParam($request, 'ext.free_trigger_source', $params);
        // 筛选【slo】【小游戏】
        $this->makeWhereParam($request, 'ext.is_bonus', $params);
        // 筛选【slo】【触发小游戏】
        $this->makeWhereParam($request, 'ext.is_bonus_trigger', $params);

        // 导出数据
        $this->export($params, $request, $export);

        // 排序分页
        $this->makeOrderParam($request, $params)->makePaginationParam($request, $params);
        // 第二排序
        $this->makeOrderParam($request, $params, 'id');
        // 统计数据
        $this->makeAggs($params, 'sum', ['usd.gain_player', 'usd.gain_sys', 'usd.bet_base', 'usd.bet_num']);
        $this->makeGroupAggs($params, 'sum', ['gain_player', 'gain_sys', 'bet_base', 'bet_num'], ['currency']);

        // 查询数据
        $results = SettlementInfo::search($params);

        // 数据列表
        $datas = $results['hits']['hits'];
        // 数据条数
        $total = $results['hits']['total']['value'];
        // 统计数据
        $stats = $this->makeStats($results['aggregations'] ?? null);

        return [
            $datas,
            $total,
            $stats
        ];
    }

    private function makeStats($aggs)
    {
        if (is_null($aggs)) {
            return ['usd' => new \stdClass(), 'sts' => []];
        }

        // USD【合计】
        $usd = [
            'gain_player' => $aggs['usd.gain_player']['value'],
            'gain_sys' => $aggs['usd.gain_sys']['value'],
            'bet_base' => $aggs['usd.bet_base']['value'],
            'bet_num' => $aggs['usd.bet_num']['value'],
        ];
        // USD【RTP】
        $usd['rtp'] = $this->makeRtp($usd['bet_num'], $usd['gain_player']);

        // 分币种【合计】
        $sts = collect($aggs['group']['buckets'])->map(function ($item) {
            return [
                'gain_player' => $item['gain_player']['value'],
                'gain_sys' => $item['gain_sys']['value'],
                'bet_base' => $item['bet_base']['value'],
                'bet_num' => $item['bet_num']['value'],
                'currency' => $item['key']['currency'],
            ];
        });
        // 分币种【RTP】
        $sts->transform(function ($item) {
            return $item + ['rtp' => $this->makeRtp($item['bet_num'], $item['gain_player'])];
        });

        return [
            'usd' => $usd,
            'sts' => $sts,
        ];
    }
}
